package com.yiyibook.controller;

import com.github.pagehelper.PageInfo;
import com.yiyibook.entity.*;
import com.yiyibook.mapper.MemberMapper;
import com.yiyibook.service.*;
import com.yiyibook.util.Constants;
import com.yiyibook.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前端主页访问 Controller
 *
 * @author yi.wu
 * @date 2019-10-14
 */
@Controller
public class IndexController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private BookBigStyleService bookBigStyleService;

    @Autowired
    private BookSmallStyleService bookSmallStyleService;

    @Autowired
    private BookService bookService;

    @Autowired
    private OrderHeadService orderHeadService;

    @RequestMapping("/model/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("yiyibook_templates/index");
        return mv;
    }

    @GetMapping(value = "/toPage")
    public ModelAndView toPage(String url,Integer pageNow,Integer pageSize,
                               String searchTitle,String keyWord, OrderHead orderCondition){
        ModelAndView mv = new ModelAndView();
        // 设置返回的视图名
        mv.setViewName(url);
        // 分页信息
        PageInfo pageInfo = new PageInfo();
        // 设置当前页
        if(pageNow != null){
            pageInfo.setPageNum(pageNow);
        }
        if(pageSize != null){
            // 设置当前页面大小
            pageInfo.setPageSize(pageSize);
        }
        // 查询条件
        Map<String,String> conditionMap = new HashMap<>(16);
        if(!Constants.Condition.CLASSIFY.equals(searchTitle) && searchTitle != null && !"".equals(searchTitle)){
            conditionMap.put(searchTitle,keyWord);
        }else{
            conditionMap = null;
        }

        // 截取 url 的 html 名称
        String htmlName = url.substring(url.lastIndexOf("/") + 1);
        // 查询所有的会员 以及分页信息
        if(Constants.Url.MEMBER.equals(htmlName)){
            ResultEntity<Member> resultEntity = memberService.selectAllMember(pageInfo, conditionMap);
            mv.addObject("memberList", resultEntity.getEntityList());
            mv.addObject("memberPageInfo", resultEntity.getPageInfo());
            // 用来 传递 查询标题 以及 关键字
            mv.addObject("memberResult", resultEntity);
        }else if(Constants.Url.BOOK_BIG_STYLE.equals(htmlName)){
            ResultEntity<BookBigStyle> resultEntity = bookBigStyleService.selectAllByCondition(pageInfo, conditionMap);
            mv.addObject("bookBigStyleList", resultEntity.getEntityList());
            mv.addObject("bookBigStylePageInfo", resultEntity.getPageInfo());
            mv.addObject("bookBigStyleResult", resultEntity);
        }else if(Constants.Url.BOOK_SMALL_STYLE.equals(htmlName)){
            ResultEntity<BookSmallStyle> resultEntity = bookSmallStyleService.selectAllByCondition(pageInfo, conditionMap);
            mv.addObject("bookSmallStyleList", resultEntity.getEntityList());
            mv.addObject("bookSmallStylePageInfo", resultEntity.getPageInfo());
            mv.addObject("bookSmallStyleResult", resultEntity);
        }else if(Constants.Url.BOOK.equals(htmlName)){
            ResultEntity<Book> resultEntity = bookService.selectAllByCondition(pageInfo, conditionMap);
            mv.addObject("bookList", resultEntity.getEntityList());
            mv.addObject("bookPageInfo", resultEntity.getPageInfo());
            mv.addObject("bookResult", resultEntity);
        }else if(Constants.Url.ORDER.equals(htmlName)){
            ResultEntity<OrderHead> orderHeadResultEntity = orderHeadService.selectOrderHeadByCondition(pageInfo, orderCondition);
        }
        return mv;
    }

    @RequestMapping("/manager/index")
    public ModelAndView managerIndex(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("yiyibook/manager/index_manager");
        return mv;
    }

    @RequestMapping("/insert/test")
    public ModelAndView insertTest(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("yiyibook/manager/index_manager");

        Member member = new Member();
        for (int i = 0; i < 30; i++) {
            member.setMemberTrueName("测试会员" + (i + 3));
            member.setMemberRole(1);
            member.setMemberName("test" + (i + 3));
            member.setMemberPassword((33333333 + i + 3) + "");
            member.setMemberPhone("" + (new Long(1391111222) * 10 + i + 4));
            member.setMemberAddress("广东省广州市某某县" + (i + 3));
            memberMapper.insertSelective(member);
        }
        return mv;
    }
}
